package hevs.eServices.client;

import java.util.ArrayList;

import hevs.eServices.client.object.Item;
import hevs.eServices.client.ui.StaticPage;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;


public class E_services_gwt_676 implements EntryPoint {

	
	
	private MySQLQueryServiceAsync mySQLService;
	
	private Label pageTitle;

	protected Grid menuGrid;
	
	protected Item item;
	
	
	
	public E_services_gwt_676() {
		mySQLService = GWT.create(MySQLQueryService.class);
		
		mySQLService.getFullListItem(new AsyncCallback<ArrayList<Item>>() {
			
			@Override
			public void onSuccess(ArrayList<Item> result) {
				menuGrid = new Grid(result.size()+2,1);
				
				for(int i = 0; i < result.size(); i++) {					
					MenuLink o1 = new MenuLink(result.get(i).getName(), result.get(i).getId());
					
					Content tempContent = new Content(result.get(i));
					
					Grid tempGrid = new Grid(2, 1);
					tempGrid.setWidget(0, 0, tempContent);
					
					o1.setPage(tempGrid);
					o1.addClickHandler(new NavHandler());

					menuGrid.setWidget(i, 0, o1);
				}
				
				RootPanel.get("smenu2").add(menuGrid);
			}
			@Override
			public void onFailure(Throwable caught) {}
		});
	}


	
	public void onModuleLoad() {

//		form = new Content();
//		createContent();

		pageTitle = new Label("Home");
		
		//MISE EN PAGE **************************************************************************************************
		RootPanel.get("object_title").setVisible(false);
		RootPanel.get("page_title").setVisible(true);
		
		RootPanel.get("content_container").add(StaticPage.getHomePage());
		RootPanel.get("title").add(pageTitle);
		//MISE EN PAGE **************************************************************************************************
		
		
		createStaticMenu();
		
//		createMenu();

	}
	
	
	// Create a static menu
	private void createStaticMenu(){	
		Grid grid = new Grid(4, 1);
		MenuLink home = new MenuLink("Home",StaticPage.getHomePage());
		home.addClickHandler(new NavHandler());
		grid.setWidget(0, 0, home);
		MenuLink info = new MenuLink("Information",StaticPage.getInfoPage());
		info.addClickHandler(new NavHandler());
		grid.setWidget(1, 0, info);
		MenuLink desc = new MenuLink("Description",StaticPage.getDescPage());
		desc.addClickHandler(new NavHandler());
		grid.setWidget(2, 0, desc);
		ExternalLink wiki = new ExternalLink("Wiki","http://e-services.wikispaces.com");
		grid.setWidget(3, 0, wiki);
		RootPanel.get("smenu1").add(grid);
	}
	
	// Create the menu for the items
	private void createMenu(){
		
		mySQLService.getListItem(new AsyncCallback<ArrayList<Item>>() {

			@Override
			public void onSuccess(ArrayList<Item> result) {
				menuGrid = new Grid(result.size()+2,1);
				
				for(int i = 0; i < result.size(); i++){					
					MenuLink o1 = new MenuLink(result.get(i).getName()+"", result.get(i).getId());
					o1.addClickHandler(new NavHandler());

					menuGrid.setWidget(i, 0, o1);
				}
				RootPanel.get("smenu2").add(menuGrid);			
			}
			
			@Override
			public void onFailure(Throwable caught) {}
		});
	}
	
//	// Create the content for the dynamic menu
//	private Grid createContent(int idItem){
//
//		mySQLService.getFullItem(idItem,new AsyncCallback<Item>() {
//
//			@Override
//			public void onSuccess(Item result) {
//				tempContent = new Content(result);
//				
//				tempGrid = new Grid(2, 1);
//				tempGrid.setWidget(0, 0, tempContent);
//				
//				System.out.println("INNER:tempContent=" + tempContent.getItem().getName());
//			}
//			
//			@Override
//			public void onFailure(Throwable caught) {}
//		});		
//		System.out.println("OUTER:tempContent=" + tempContent.getItem().getName());
//		
//		return tempGrid;
//	}
	
	// Event handler for the menu
	private class NavHandler implements ClickHandler{
		
		@Override
		public void onClick(ClickEvent event) {
			MenuLink a = (MenuLink) event.getSource();
			pageTitle.setText(a.getText());
//			a.setPage(createContent(a.getId()));
			
			//MISE EN PAGE **************************************************************************************************
			if(a.getId()!=0){
				RootPanel.get("object_title").setVisible(true);
				RootPanel.get("page_title").setVisible(false);
			}else{
				RootPanel.get("object_title").setVisible(false);
				RootPanel.get("page_title").setVisible(true);
			}
			//MISE EN PAGE **************************************************************************************************
			
			RootPanel.get("content_container").clear();

			RootPanel.get("content_container").add(a.getPage());
		}	
	}
}
